package cz.matasek.dbLogger.gui;

import cz.matasek.dbLogger.model.Database;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.util.HashSet;
import java.util.Set;

/**
 * This class is the main application frame. Uses the singleton design pattern.
 *
 * @author Luboš Matásek
 */
public class Gui extends javax.swing.JFrame {

    /**
     * Serial version UID.
     */
    private static final long serialVersionUID = -5608365424927642086L;
    /**
     * The GUI.
     */
    private static Gui gui;
    /**
     * The database.
     */
    private Database database;
    /**
     * Set of objects implementing refreshable interface.
     */
    public Set<Refreshable> refreshableObjects;

    /** 
     * Creates a new <code>Gui</code>.
     */
    private Gui() {
        refreshableObjects = new HashSet<Refreshable>();

        initComponents();

        _tables.setLayout(new GridLayout());
        _query.setLayout(new GridLayout());
        _history.setLayout(new GridLayout());

        setDisconnected();
    }

    /**
     * Returns the only instance of the GUI.
     *
     * @return the GUI
     */
    public static Gui getInstance() {
        if (gui == null) {
            gui = new Gui();
        }

        return gui;
    }

    /**
     * Sets the database object.
     *
     * @param database the database
     */
    public void setDatabase(Database database) {
        this.database = database;
    }

    /**
     * Sets the GUI into connected state (shows all application panels).
     */
    public void setConnected() {
        DatabaseInfoPanel databaseInfoPanel = new DatabaseInfoPanel(database);
        _database.setLayout(new GridLayout());
        _database.removeAll();
        _database.add(databaseInfoPanel);
        refreshableObjects.add(databaseInfoPanel);

        TablesPanel tablesPanel = new TablesPanel(database);
        _tables.removeAll();
        _tables.add(tablesPanel);
        _tabbedPane.setEnabledAt(1, true);
        refreshableObjects.add(tablesPanel);

        QueryPanel queryPanel = new QueryPanel(database);
        _query.removeAll();
        _query.add(queryPanel);
        _tabbedPane.setEnabledAt(2, true);
        refreshableObjects.add(queryPanel);

        HistoryPanel historyPanel = new HistoryPanel(database);
        _history.removeAll();
        _history.add(historyPanel);
        _tabbedPane.setEnabledAt(3, true);
        refreshableObjects.add(historyPanel);
    }

    /**
     * Sets the GUI into disconnected state (hides all application panels).
     */
    public void setDisconnected() {
        refreshableObjects.clear();

        _database.setLayout(new GridBagLayout());
        _database.removeAll();
        _database.add(new ConnectionPanel());

        _tabbedPane.setEnabledAt(1, false);
        _tabbedPane.setEnabledAt(2, false);
        _tabbedPane.setEnabledAt(3, false);

        database = null;
    }

    /**
     * Refreshes content of the application.
     */
    public void refresh() {
        database.updateMetadata();

        for (Refreshable object : refreshableObjects) {
            object.refresh();
        }

        repaint();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        _tabbedPane = new javax.swing.JTabbedPane();
        _database = new javax.swing.JPanel();
        _tables = new javax.swing.JPanel();
        _query = new javax.swing.JPanel();
        _history = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Database Logger");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setLocationByPlatform(true);
        setMinimumSize(new java.awt.Dimension(800, 600));

        javax.swing.GroupLayout _databaseLayout = new javax.swing.GroupLayout(_database);
        _database.setLayout(_databaseLayout);
        _databaseLayout.setHorizontalGroup(
            _databaseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 795, Short.MAX_VALUE)
        );
        _databaseLayout.setVerticalGroup(
            _databaseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 572, Short.MAX_VALUE)
        );

        _tabbedPane.addTab("Database", _database);

        javax.swing.GroupLayout _tablesLayout = new javax.swing.GroupLayout(_tables);
        _tables.setLayout(_tablesLayout);
        _tablesLayout.setHorizontalGroup(
            _tablesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 795, Short.MAX_VALUE)
        );
        _tablesLayout.setVerticalGroup(
            _tablesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 572, Short.MAX_VALUE)
        );

        _tabbedPane.addTab("Tables", _tables);

        javax.swing.GroupLayout _queryLayout = new javax.swing.GroupLayout(_query);
        _query.setLayout(_queryLayout);
        _queryLayout.setHorizontalGroup(
            _queryLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 795, Short.MAX_VALUE)
        );
        _queryLayout.setVerticalGroup(
            _queryLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 572, Short.MAX_VALUE)
        );

        _tabbedPane.addTab("Queries", _query);

        _history.setMinimumSize(new java.awt.Dimension(595, 372));
        _history.setLayout(new java.awt.GridLayout(1, 1));
        _tabbedPane.addTab("History", _history);
        _history.getAccessibleContext().setAccessibleParent(this);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(_tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(_tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 600, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel _database;
    private javax.swing.JPanel _history;
    private javax.swing.JPanel _query;
    private javax.swing.JTabbedPane _tabbedPane;
    private javax.swing.JPanel _tables;
    // End of variables declaration//GEN-END:variables
}
